package com.ants.modules.system.service.impl;

import com.ants.common.constant.CacheConstant;
import com.ants.common.system.api.ISysBaseAPI;
import com.ants.common.system.vo.*;
import com.ants.common.utils.oConvertUtils;
import com.ants.modules.extension.mapper.ExtensionMapper;
import com.ants.modules.quartz.service.IQuartzJobService;
import com.ants.modules.system.entity.*;
import com.ants.modules.system.mapper.*;
import com.ants.modules.system.service.ISysDictService;
import com.google.common.base.Joiner;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * TODO
 * Author Chen
 * Date   2021/9/7 18:44
 */
@Slf4j
@Service
public class SysBaseApiImpl implements ISysBaseAPI {
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private ISysDictService sysDictService;
    @Resource
    private WxConfigMapper wxConfigMapper;
    @Resource
    private ExtensionMapper extensionMapper;
    @Resource
    private AntsSystemConfigMapper antsSystemConfigMapper;
    @Autowired
    private IQuartzJobService quartzJobService;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
//    @Cacheable(cacheNames = CacheConstant.SYS_USERS_CACHE, key = "#username")
    public LoginUser getUserByName(String username) {
        if (oConvertUtils.isEmpty(username)) {
            return null;
        }
        LoginUser loginUser = new LoginUser();
        SysUser sysUser = userMapper.getUserByName(username);
        if (sysUser == null) {
            return null;
        }
        BeanUtils.copyProperties(sysUser, loginUser);
        return loginUser;
    }

    @Override
    public List<String> queryTableDictByKeys(String table, String text, String code, String[] keyArray) {
        return sysDictService.queryTableDictByKeys(table, text, code, Joiner.on(",").join(keyArray));
    }

    @Override
    public List<String> getRolesByUsername(String username) {
        return sysUserRoleMapper.getRoleByUserName(username);
    }

    @Override
    public List<String> getRoleNamesByUsername(String username) {
        return sysUserRoleMapper.getRoleNamesByUsername(username);
    }

    @Override
    public Map<String, Object> getWxConfig() {
        return wxConfigMapper.getWxConfig();
    }

    @Override
    public List<Map<String, String>> getExtensionList() {
        return extensionMapper.getExtensionList();
    }

    @Override
    public Map<String, Object> getAntsSystemConfig() {
        return antsSystemConfigMapper.getConfig();
    }

    @Override
    public void executeJob(String id) {
        try {
            quartzJobService.execute2(id);
        } catch (Exception e) {

        }
    }

}
